package com.gy.dalian.controller;

import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.operator.Equal;
import com.ejlchina.searcher.util.MapUtils;
import com.gy.dalian.common.AjaxResult;
import com.gy.dalian.common.BaseController;
import com.gy.dalian.model.DldpIsAuto;
import com.gy.dalian.model.DldpTable;
import com.gy.dalian.service.DldpIsAutoService;
import com.gy.dalian.service.DldpTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author:23562
 * @date:2023/3/18
 */
@Controller
@RequestMapping("/dw-service-xxl-screen/DldpIsAutoController")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class DldpIsAutoController extends BaseController {

    private final String CITY = "2";
    private final DldpIsAutoService dldpIsAutoService;
    private final DldpTableService dldpTableService;
    private final BeanSearcher beanSearcher;

    @PostMapping("getIsAutoList")
    @ResponseBody
    public Map<String, Object> getIsAutoList() {
        DldpIsAuto dldpIsAuto = new DldpIsAuto();
        dldpIsAuto.setCity(CITY);
        List<DldpIsAuto> list = dldpIsAutoService.queryInfo(dldpIsAuto);

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("table", list);
        map.put("data", map2);
        map.put("code", 200);
        return map;
    }

    @PostMapping("getTableData")
    @ResponseBody
    public Map<String, Object> getTableData(String table, HttpServletRequest request, HttpServletResponse response) {
        String tableName = request.getParameter("tableName");
        DldpTable dldpTable = new DldpTable();
        dldpTable.setTableName(tableName);
        dldpTable.setCityCode(CITY);
        List<DldpTable> dldpTableList = dldpTableService.queryInfo(dldpTable);

        String sql = "select  *, " + "concat('" + tableName + "') tableName from " + tableName;
        List<LinkedHashMap<String, Object>> list = dldpTableService.queryInfoBySql(sql);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("table", list);
        map2.put("head", dldpTableList);
        map.put("data", map2);
        map.put("code", 200);
        return map;
    }

    @RequestMapping("addSave")
    @ResponseBody
    public ResponseEntity<AjaxResult> addSave(HttpServletRequest request, HttpServletResponse response) {
        String tableName = request.getParameter("tableName");
        String id = request.getParameter("id");
        Map<String, Object> params = MapUtils.builder()
                .field(DldpTable::getId, id).op(Equal.class).build();
        int num = beanSearcher.searchCount(DldpTable.class, params).intValue();
        DldpTable dldpTable = new DldpTable();
        dldpTable.setTableName(tableName);
        dldpTable.setCityCode(CITY);
        List<DldpTable> tableList = dldpTableService.queryInfo(dldpTable);
        StringBuilder sql = new StringBuilder();
        if (num > 0) {
            return updateInfo(request, sql, tableList, tableName, id);
        } else {
            sql.append("insert into  ").append(tableName).append("(");
            for (DldpTable table : tableList) {
                if (!"tableName".equals(table.getField())) {
                    sql.append(table.getField()).append(",");
                }
            }
            System.err.println(tableName);
//            sql.deleteCharAt(sql.length() - 1);
//            sql.append(" where id = ").append(id);
            System.err.println(sql);
        }
        return ResponseEntity.ok(error());
    }

    private ResponseEntity<AjaxResult> updateInfo(HttpServletRequest request, StringBuilder sql, List<DldpTable> list, String tableName, String id) {
        sql.append("update ").append(tableName).append(" set ");
        for (DldpTable table : list) {
            if (!Objects.equals("tableName", table.getField())) {
                sql.append(table.getField()).append("=" + "'").append(request.getParameter(table.getField())).append("'").append(",");
            }
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" where id = ").append(id);
        int num = dldpTableService.updateBySql(sql.toString());
        return num > 0 ? ResponseEntity.ok(success()) : ResponseEntity.ok(error());
    }
}
